<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>
    <script src="./js/axios.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .tr {
            display: flex;
        }

        .tr .td {
            flex: 1;
            text-align: center;
            line-height: 40px;
            background-color: black;
            color: white;
        }
    </style>
</head>

<body>
    <div id="app">
        <div>
            <div>用户名 <input v-model="username" /></div>
            <div>年龄 <input v-model="userage" /></div>
            <button @click="add"> 新增</button>
        </div>
        <hr />
        <div>
            用户名 <input v-model="key" />
        </div>
        <div class="tr">
            <div class="td">编号</div>
            <div class="td">姓名</div>
            <div class="td">年龄</div>
            <div class="td">操作</div>
        </div>
        <div class="tr" v-for="item  in list">
            <div class="td">{{item.id}}</div>
            <div class="td">{{item.username}}</div>
            <div class="td">{{item.userage}}</div>
            <div class="td" @click="delById(item.id)">删除</div>
        </div>
    </div>

</body>
<script>
    new Vue({
        el: "#app",
        data: {
            key: "",//搜索的关键字
            username: "",
            userage: "",
            list: [],
            timer: null,
        },
        watch: {//监听
            key() {
                this.search();
            }
        },
        async created() {
            let { data: list } = await axios({
                method: "get",
                url: "http://localhost:2345/userinfo"
            })
            this.list = list;
        },
        methods: {
            search() {
                clearInterval(this.timer);
                this.timer = setTimeout(async () => {
                    let { data: list } = await axios({
                        method: "get",
                        url: "http://localhost:2345/userinfo",
                        params: {
                            "username_like": this.key
                        }
                    })
                    this.list = list;
                },500);
            },


            async delById(id) {
                await axios({
                    method: "delete",
                    url: "http://localhost:2345/userinfo/" + id
                })
                this.list = this.list.filter(item => item.id !== id);
            },
            async add() {
                let { data } = await axios({
                    method: "post",
                    url: "http://localhost:2345/userinfo",
                    data: {
                        username: this.username,
                        userage: this.userage
                    }
                })
                //填充数据
                this.list.push(data);

            }
        }
    })

</script>

</html>